package com.alumni_circle.core.utils;

import com.alumni_circle.constant.PathConstant;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * 传输url,openid
 * 使用链接下载图片到本项目下的img目录，并返回此图片路径
 * 
 * 传输List<String>url 用于下载轮播图的图片
 * 
 * @author 龙小虬
 * @since 2020-07-09 14:54
 */
public class DownloadPicture {

//    public static void main(String[] args) throws IOException{
////        System.out.println(downloadPicture("http://pic25.nipic.com/20121113/9252150_105400798000_2.jpg","1"));
//        List<String> list = new ArrayList<>();
//        list.add("http://pic25.nipic.com/20121113/9252150_105400798000_2.jpg");
//        System.out.println(slideShowPicture(list));
//    }
    /**
     * 下载用户头像
     * @param urlIcon 用户头像链接
     * @param open openid
     * @return 用户头像目录（openid.png）
     * @throws IOException
     */
    public static String downloadPicture(String urlIcon,String open) throws IOException{
        //设定为当前文件夹
        File directory = new File("");
        URL url = null;
        String imageName = null;

        try {
            url = new URL(urlIcon);
            DataInputStream dataInputStream = new DataInputStream(url.openStream());
            imageName =  PathConstant.SERVER +open+".png";
//            System.out.println(imageName);

            FileOutputStream fileOutputStream = new FileOutputStream(new File(imageName));
            ByteArrayOutputStream output = new ByteArrayOutputStream();

            byte[] buffer = new byte[1024];
            int length;

            while ((length = dataInputStream.read(buffer)) > 0) {
                output.write(buffer, 0, length);
            }
            fileOutputStream.write(output.toByteArray());
            dataInputStream.close();
            fileOutputStream.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return open+".png";
    }

    /**
     * 下载轮播图图片
     * @param urlPicture
     * @return 返回轮播图的全部路径(当前时间+?+i.png)
     * @throws IOException
     */

    public static List<String> slideShowPicture(List<String> urlPicture) throws IOException{
        //设定为当前文件夹
        File directory = new File("");
        URL url = null;
        int imageNumber = 0;
        List<String> list = new ArrayList<String>();
        for (int i = 0; i < urlPicture.size(); i++) {
            String imageName = null;
            try {
                url = new URL(urlPicture.get(i));
                DataInputStream dataInputStream = new DataInputStream(url.openStream());
                imageName =  directory.getAbsolutePath()+"/img/"+ StringTime.time()+"?"+i+".png";
//            System.out.println(imageName);

                FileOutputStream fileOutputStream = new FileOutputStream(new File(imageName));
                ByteArrayOutputStream output = new ByteArrayOutputStream();

                byte[] buffer = new byte[1024];
                int length;

                while ((length = dataInputStream.read(buffer)) > 0) {
                    output.write(buffer, 0, length);
                }
                fileOutputStream.write(output.toByteArray());
                dataInputStream.close();
                fileOutputStream.close();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            list.add(imageName);
        }
        return list;
    }

}
